// CSVReader.h: interface for the CCSVParser class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CSVREADER_H__6C372692_ECD6_4C83_AFD6_EA5DA50B1879__INCLUDED_)
#define AFX_CSVREADER_H__6C372692_ECD6_4C83_AFD6_EA5DA50B1879__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

namespace parsers
{

/*!\brief A CSV file reader

This class is used to scan CSV file: text file containing columns of numbers.

\par Delimitator:

  The accepted separators are, space (" ") or tab ("\t") and ",;:".

\par File structure:

  The file must have the following structure(| denotes a delimitator):
\begincode
name1 | name2 | ... | namen
number11 | number12 | ... | number1n
...
numberi1 | ..... | numberi3 | ...
  \endcode

There can be a arbitrary number of lines. Moreover, if a line is not complete, 0 are added.

\ingroup Algotools group
*/
class CCSVParser  
{
public:
	//! \name Typedefs
	//@{
	typedef std::vector< float > Vector;
	typedef std::pair< CString, Vector > NamedVector;
	typedef std::vector< NamedVector > NamedVectorContainer;
	//@}

public:
	//! \name Constructor
	//@{
	CCSVParser();
	virtual ~CCSVParser();
	//@}

	//! \name Methods
	//@}
	//! Sets the delimitators
	void SetDelimitators( LPCTSTR szDelims )	{	m_sDelims = szDelims;};
	//! Returns the delimitators	
	LPCTSTR GetDelimitators() const				{	return m_sDelims;};
	/*! \name Parse file

	\param szFileName file name of the file to parse
	\return true if successful, false otherwize
	*/
	bool ParseFile( LPCTSTR szFileName );
	/*! \name Parse string

	\param szString null-terminated string to parse
	\return true if successful, false otherwize	
	*/
	bool ParseString( LPCTSTR szString );
	//@}

	//! \name Result and conversion
	//@}
	const NamedVectorContainer& GetVectors() const	{	return m_vectors;};
	/*!\name  Convert back to string

	\param str resulting string
	\param szEOL end of line string: can be "\n" or "\r\n"
	*/
	void ToString(CString& str, LPCTSTR szEOL = _T("\r\n"));
	//@}

protected:
	// reads the column names
	LPCTSTR ReadHeaders( LPCTSTR szLine);
	// reads 1 line
	LPCTSTR ReadLine( LPCTSTR szLine);

private:
	CString m_sDelims;
	NamedVectorContainer m_vectors;
	static LPCTSTR m_szNumbers;
};

};

#endif // !defined(AFX_CSVREADER_H__6C372692_ECD6_4C83_AFD6_EA5DA50B1879__INCLUDED_)
